package cn.tedu.cn.tedu.homework;

import java.sql.*;
import java.util.HashMap;
import java.util.Scanner;

/**
 * @author :Colin
 * @version 创建时间: 2021/7/3 9:42
 */
public class LogIn {
    public static void main(String[] args) throws Exception {

       int result;
        do{
            System.out.println("请输入用户名");
            String userName = new Scanner(System.in).nextLine();
            System.out.println("请输入密码");
            String userPsw = new Scanner(System.in).nextLine();
            result=checkInfo(userName,userPsw);
        }while(result!=1);


    }

    private static int checkInfo(String userName,String userPsw) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/colinbase";
        Connection conn = DriverManager.getConnection(url, "root", "root");
        Statement st = conn.createStatement();
        String sql="select * from user";
        ResultSet rs = st.executeQuery(sql);
        HashMap<String, String> map = new HashMap<String, String>();
        while(rs.next()){
            String name = rs.getString(2);
            String password = rs.getString(3);
            map.put(name,password);
        }
        rs.close();
        st.close();
        conn.close();
        boolean blName = map.containsKey(userName);
        if(blName){
            String passCode = map.get(userName);
            System.out.println("你的用户名是"+userName);
            if(userPsw.equals(passCode)){
                System.out.println("登录成功");
                return 1;

            }else{
                System.out.println("密码错误");
                return 0;
            }
        }else {
            System.out.println("用户不存在");
            return 0;
        }

    }
}
